package main.Concurrent.Iteration;

import main.Concurrent.Iteration.IterationOperation;

import com.googlecode.actorom.Actor;
import com.googlecode.actorom.Address;
import com.googlecode.actorom.KillActorException;
import com.googlecode.actorom.Topology;
import com.googlecode.actorom.annotation.OnMessage;
import com.googlecode.actorom.annotation.TopologyInstance;

public class IterationOperationHandler<E, R, A> {
    @TopologyInstance
    private Topology topology;
    private Address runnerAddress;
    
    private IterationOperation<E, R, A> op;
    
    @OnMessage(type = IterationOperationConstructionMessage.class)
    public void construct(IterationOperationConstructionMessage<E, R, A> m) {
        this.runnerAddress = m.getRunnerAddress();
        this.op = m.getOperation();
    }
    
    @OnMessage(type = IterationOperationRunMessage.class)
    public void onRun(IterationOperationRunMessage<E> message) {
        R res = this.op.compute(message.getIndex(), message.getValue());
        Actor parent = topology.getActor(runnerAddress);
        parent.send(new IterationOperationDoneMessage<R>(message.getIndex(), res));
    }
}
